package com.ruoyi.report_chl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_chl.domain.bo.ChlFmhxdBsInfoBo;
import com.ruoyi.report_chl.domain.vo.ChlFmhxdBsInfoVo;
import com.ruoyi.report_chl.domain.ChlFmhxdBsInfo;
import com.ruoyi.report_chl.mapper.ChlFmhxdBsInfoMapper;
import com.ruoyi.report_chl.service.IChlFmhxdBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粉煤灰细度试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-15
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ChlFmhxdBsInfoServiceImpl implements IChlFmhxdBsInfoService {

    private final ChlFmhxdBsInfoMapper baseMapper;

    /**
     * 查询粉煤灰细度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ChlFmhxdBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询粉煤灰细度试验检测记录列表
     */
    @Override
    public TableDataInfo<ChlFmhxdBsInfoVo> queryPageList(ChlFmhxdBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ChlFmhxdBsInfo> lqw = buildQueryWrapper(bo);
        Page<ChlFmhxdBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粉煤灰细度试验检测记录列表
     */
    @Override
    public List<ChlFmhxdBsInfoVo> queryList(ChlFmhxdBsInfoBo bo) {
        LambdaQueryWrapper<ChlFmhxdBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChlFmhxdBsInfo> buildQueryWrapper(ChlFmhxdBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ChlFmhxdBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ChlFmhxdBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ChlFmhxdBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ChlFmhxdBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ChlFmhxdBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ChlFmhxdBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ChlFmhxdBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ChlFmhxdBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ChlFmhxdBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ChlFmhxdBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ChlFmhxdBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ChlFmhxdBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ChlFmhxdBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ChlFmhxdBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ChlFmhxdBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ChlFmhxdBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ChlFmhxdBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ChlFmhxdBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ChlFmhxdBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ChlFmhxdBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ChlFmhxdBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ChlFmhxdBsInfo::getFujsm, bo.getFujsm());

        return lqw;
    }

    /**
     * 新增粉煤灰细度试验检测记录
     */
    @Override
    public Boolean insertByBo(ChlFmhxdBsInfoBo bo) {
        ChlFmhxdBsInfo add = BeanUtil.toBean(bo, ChlFmhxdBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改粉煤灰细度试验检测记录
     */
    @Override
    public Boolean updateByBo(ChlFmhxdBsInfoBo bo) {
        ChlFmhxdBsInfo update = BeanUtil.toBean(bo, ChlFmhxdBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChlFmhxdBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粉煤灰细度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
